﻿using System.Collections.Generic;
using MvcForum.Net.Business.Utils;
using MvcForum.Net.Enums;
using MvcForum.Net.Utils;

namespace MvcForum.Net.Business
{
    public class ForumsRepository
    {

        public static List<ForumsListEntity> List(int parentId, int maxRecursion, bool _checkVisibleAtHomePage)
        {

            MainDB _db = new MainDB();

            return _db.ForumList(parentId, maxRecursion, _checkVisibleAtHomePage).ExecuteTypedList<ForumsListEntity>();


        }



        public static List<ForumsListEntity> ForumJump()
        {

            var _data = CacheWaper.Get(Contrains.CACHE_FORUM_JUMP);
            if (_data == null)
            {

                _data = List(0, 99, false);
                CacheWaper.Add(Contrains.CACHE_FORUM_JUMP, _data, ForumContext.Configuration.SecondsInCache);

            }

            return _data as List<ForumsListEntity>;

        }



        public static Forum ForumDetail(int forumId)
        {

            var _list = new MainDB().ForumDetail(forumId).ExecuteTypedList<Forum>();
            if (_list.Count > 0)
                return _list[0];
            return null;

        }


        public static List<Forum> SubToParent(int forumId)
        {

            var _db = new MainDB();

            return _db.ForumSubToParent(forumId).ExecuteTypedList<Forum>();


        }


        public static List<Smile> SmileList()
        {

            var _db = new MainDB();
            return _db.SmilesList().ExecuteTypedList<Smile>();


        }

        public static void UpdateLastPost(int parentId,int topics,int replies, string userName,int postId,string subject)
        {


            var _db = new MainDB();

            _db.ForumUpdateLastPost(parentId, topics, replies, userName, postId, subject).Execute();

            //var _forums = SubToParent(parentId);

            //_forums.Reverse();

            //var _db = new MainDB();

            //foreach (var forum in _forums)
            //{
            //    _db.ForumUpdateLastPost(forum.ForumId, userName, postId, subject).Execute();
            //}
            
        }


        public static StatisticEntity Statics(int min)
        {
            MainDB _db = new MainDB();
            var _list = _db.ForumStatistics(min).ExecuteTypedList<StatisticEntity>();
            if (_list.Count > 0)
                return _list[0];
            return null;
        }


        public static Statistic TimeMaxUsersOnline()
        {
            MainDB _db = new MainDB();
            var _list = _db.StatisticMaxOnline().ExecuteTypedList<Statistic>();
            if (_list.Count > 0)
                return _list[0];
            return null;
        }



        public static List<TimeZone> TimeZoneList()
        {
            return new MainDB().TimeZoneList().ExecuteTypedList<TimeZone>();
        }


    }
}
